package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.Bfjltb1Service;
import cn.e4j.modular.api.service.Gwat151Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.crm.entity.GwbnT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 非商机拜访记录
 * Created by wenchuan.li on 2019/03/26.
 */
@RestController
@RequestMapping("api/wx/bfjltb1")
public class Bfjltb1Controller {

    private static Logger logger = LoggerFactory.getLogger(Bfjltb1Controller.class);
    @Autowired
    private Bfjltb1Service bfjltb1Service;

    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    private Gwat151Service gwat151Service;

    @Autowired
    GwbService gwbService;

    /**
     * 拜访记录提报作业--获取拜访记录列表
     *
     * @return
     */
    @RequestMapping(value = "getBfjltb1List")
    public ResponseData getBfjltb1List(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWBNENT", inDto.getString("ENT"));
            inDto.put("GWBNUSER", currentUser.getID());

            //客户权限
            inDto.put("GWAK006", "1");
            Dto gwb001 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list = (List<Dto>) gwb001.get("CUSTOMERS");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(currentUser,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);

            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            List<Dto> listOrg = (List<Dto>)sys003.get("ORGS");
            listOrg.remove(listOrg.size()-1);
            inDto.put("ORGS", listOrg);
            list = InvokeUtil.invokeBeanMethod("bfjltb1Mapper,getBfjltb1List",inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务开小差了!");
        }
        return new SuccessResponseData(list);
    }

    /**
     * 拜访记录--新增
     *
     * @return
     */
    @RequestMapping(value = "bfjltb1Add")
    public ResponseData bfjltb1Add(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        GwbnT gwbn_tPO = null;
        SysUser currentUser = wapper.getSysUser();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("_jsonStr_"));

            JSONObject data = inJson.getJSONObject("data");
            gwbn_tPO = data.toJavaObject(GwbnT.class);
            gwbn_tPO.setGWBNENT(currentUser.getDBC());
            gwbn_tPO.setGWBN001(currentUser.getID());
            gwbn_tPO.setGWBNID(data.getString("GWAK001"));
            gwbn_tPO.setGWBNTIME(DateUtils.getTime());
            gwbn_tPO.setGWBNDATE(LocalDate.now());
            gwbn_tPO.setGWBNUSER(currentUser.getID());
            gwbn_tPO.setGWBNORIG(currentUser.getORG_ID());
            gwbn_tPO.setGWBNGRUP(currentUser.getORG_ID());
            gwbn_tPO.setGWBNACTI("N");

            // 项次+1
            Dto gwbnMaxDto = new Dto();
            gwbnMaxDto.put("GWBNENT", inDto.getString("ENT"));
            gwbnMaxDto.put("GWBNID", gwbn_tPO.getGWBNID());
            List<Dto> maxlist = bfjltb1Service.getGwbnseqMax(gwbnMaxDto);
            if (maxlist.size() > 0 && ToolUtil.isNotEmpty(maxlist.get(0))) {
                Dto maxDto = maxlist.get(0);
                gwbn_tPO.setGWBNSEQ(maxDto.getBigDecimal("GWBNSEQ").add(BigDecimal.ONE));
            } else {
                gwbn_tPO.setGWBNSEQ(BigDecimal.ONE);
            }

            bfjltb1Service.bfjltb1Add(gwbn_tPO);

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务开小差了!");
        }
        return new SuccessResponseData();
    }


    /**
     * 拜访记录--修改
     *
     * @return
     */
    @RequestMapping(value = "bfjltb1Edit")
    public ResponseData bfjltb1Edit(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        GwbnT gwbn_tPO = null;
        SysUser currentUser = wapper.getSysUser();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("_jsonStr_"));

            JSONObject data = inJson.getJSONObject("data");
            gwbn_tPO = data.toJavaObject(GwbnT.class);
            gwbn_tPO.setGWBNENT(currentUser.getDBC());
            gwbn_tPO.setGWBNMODU(currentUser.getID());

            bfjltb1Service.bfrPage01Edit(gwbn_tPO);

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务开小差了!");
        }
        return new SuccessResponseData();
    }


    /**
     * 删除拜访记录
     * <p>
     *
     * @return
     */
    @RequestMapping(value = "delBfjl")
    public ResponseData delBfjl(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            inDto.put("GWBNENT", inDto.getString("ENT"));
            inDto.put("GWBNACTI", "Y");
            bfjltb1Service.delBfjl(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务开小差了!");
        }
        return new SuccessResponseData();
    }

    /**
     * 获取客户列表
     *
     * @return
     */
    @RequestMapping(value = "getGwakList")
    public ResponseData getGwakList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        List<Dto> list = new ArrayList<>();
        inDto.put("GWAKENT", inDto.getString("ENT"));
        inDto.put("GWAKACTI", "Y");
        try {

            //客户权限
            inDto.put("GWAK006", "1");
            Dto gwb001 = gwbService.getGwb001(user,inDto);
            List<Dto> _list = (List<Dto>) gwb001.get("CUSTOMERS");
            inDto.put("CUSTOMERS", _list);

            //经销商权限
            inDto.put("GWAK006", "2");
            Dto gwb001_1 = gwbService.getGwb001(user,inDto);
            List<Dto> _list1 = (List<Dto>) gwb001_1.get("CUSTOMERS");
            inDto.put("DISTRIBUTORS", _list1);


            //上下级权限--取客户不需要此权限
//            Dto sys003 = systemService.getSys003();
//            inDto.put("orgs", sys003.get("orgs"));

            list=  bfjltb1Service.getGwakList(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData("服务开小差了!");
        }
        return new SuccessResponseData(list);
    }


}
